package com.fadadianqi.mall.web.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.mall.model.Nav;
import com.fadadianqi.mall.service.NavService;
import com.yeqiangwei.commons.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by aroot on 2/25/14.
 */
@Controller
public class ProductController {

    @Autowired
    private NavService navService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/mall/ajax/getBrand")
    public ResponseEntity<String> getBrand(HttpServletRequest request, ModelMap modelMap) {
        Brand brand = goodsService.getBrandById(ParamUtils.getString(request, "id", null));
        Boolean isSucceed = (brand != null);
        AjaxResult rs = new AjaxResult(brand, isSucceed, null);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    @RequestMapping(value = "/mall/ajax/getNav")
    public ResponseEntity<String> getNav(HttpServletRequest request, ModelMap modelMap) {
        Nav nav = navService.getById(ParamUtils.getString(request, "id", null));
        Boolean isSucceed = (nav != null);
        AjaxResult rs = new AjaxResult(nav, isSucceed, null);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }
}
